package com.wolfeyes.common.utils.serialize;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

//import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wolfeyes.common.utils.character.ParamUtils;

/**
 * @description 对象序列化与反序列化-Util工具类
 * @author yanyljava
 * @email yanyljava@qq.com
 * @version V1.0
 */
public class SerializeUtils {

	/**
	 * slf4j日志对象
	 */
    private static final Logger logger = LoggerFactory.getLogger(SerializeUtils.class);

    /**
     * @description 序列化-serialize-toBytes
     * @param object 待序列化的对象
     * @author yanyl
     * @since 2018年10月08日上午13:37:39
     * @return byte[] 存储对象的字节数组
     * @throws Exception 异常
     */
    public static byte[] serialize(Object object) {

        byte[] result = null;

        if (object == null) {
            return new byte[0];
        }
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(128);
            try  {
            	
            	// 未实现实例化接口-Serializable
                if (!(object instanceof Serializable)) {
                	
                    throw new IllegalArgumentException(SerializeUtils.class.getSimpleName() + " requires a Serializable payload " +
                            "but received an object of type [" + object.getClass().getName() + "]");
                }
                
                // 创建对象输出流objectOutputStream
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                objectOutputStream.writeObject(object);
                objectOutputStream.flush();
                result =  byteArrayOutputStream.toByteArray();
            }
            catch (Throwable ex) {
                throw new Exception("Failed to serialize", ex);
            }
        } catch (Exception ex) {
            logger.error("Failed to serialize",ex);
        }
        return result;
    }
    
    /**
     * @description 反序列化-deserialize-getObjectFromBytes
     * @param bytes 存储对象的字节数组
     * @author yanyl
     * @since 2018年10月08日上午13:37:39
     * @return Object 反序列化后获得的对象
     * @throws Exception 异常
     */
    public static Object deserialize(byte[] bytes) {

        Object result = null;

        // 判断byte[]数组是否为空；
        if (ParamUtils.isEmpty(bytes)) {
            return null;
        }

		try {

			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

			try {

				// 创建对象输入流objectInputStream
				ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
				try {
					result = objectInputStream.readObject();
				} catch (ClassNotFoundException ex) {
					throw new Exception("Failed to deserialize object type", ex);
				}
			} catch (Throwable ex) {
				throw new Exception("Failed to deserialize", ex);
			}
		} catch (Exception e) {
			logger.error("Failed to deserialize", e);
		}
		
        return result;
    }

}

